//
//  LineDetailViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LineDetailViewController.h"
#import "ZWYNetTool.h"
#import "TravelItem.h"
#import "LineModel.h"
#import "MyLayout.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "Piclist.h"
#import "UserInfo.h"
#import "LPJCollectionCell.h"
#import "LineDetailTableCell.h"
#import "Masonry.h"
#import "LineReplyTableCell.h"
#import "LineDefaultTableCell.h"
#import "PhotoAlbumViewController.h"

#define kLPJCollectionCell @"LPJCollectionCellIdentifier"
#define kLineDetailTableCell @"LineDetailTableCellIdentifier"
#define kLineReplyTableCell @"LineReplyTableCellIdentifier"
#define kLineDefaultTableCell @"LineDefaultTableCellIdentifier"

@interface LineDetailViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    UICollectionViewDataSource,
    UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout
>
@property (nonatomic, strong) TravelItem *travelItem;
@property (nonatomic, strong) LineModel *lineModel;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nickNameLabel;
@property (nonatomic, strong) UIImageView *genderImage;
@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UILabel *maritalStatusLabel;
@property (nonatomic, strong) UILabel *wantGoLabel;
@property (nonatomic, strong) UILabel *remarkLabel;
@property (nonatomic, strong) UIImageView *imageView1;
@property (nonatomic, strong) UIImageView *imageView2;
@property (nonatomic, strong) UIImageView *imageView3;
@property (nonatomic, strong) UIImageView *imageView4;
@property (nonatomic, strong) UIImageView *imageView5;
@property (nonatomic, strong) UIImageView *imageView6;
@property (nonatomic, strong) UIImageView *imageView7;
@property (nonatomic, strong) UIImageView *imageView8;
@property (nonatomic, strong) UIImageView *imageView9;
@property (nonatomic, strong) NSArray<Piclist *> *picList;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) UIButton *replyButton;
@property (nonatomic, strong) UIButton *joinButton;
@property (nonatomic, strong) UICollectionView *LPJCollection;  // like + reply + join
@property (nonatomic, strong) UILabel *LPJInfoLabel;
@property (nonatomic, strong) NSArray<UserInfo *> *userArray;
@property (nonatomic, strong) NSArray<NSArray *> *detailTitleArray;
@property (nonatomic, strong) NSArray<NSArray *> *detailValueArray;

@end

@implementation LineDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createData];
}

#pragma mark - View

- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.dk_width, self.view.dk_height - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    
    [self createTableHeaderView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)createTableHeaderView {
    MyLinearLayout *rootLinearLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    rootLinearLayout.padding = UIEdgeInsetsMake(0, 10, 10, 10);
    rootLinearLayout.frame = CGRectMake(0, 0, CGRectGetWidth(self.tableView.bounds), 0);
    rootLinearLayout.myLeftMargin = rootLinearLayout.myRightMargin = 0;
    rootLinearLayout.backgroundColor = [UIColor whiteColor];
    
    MyRelativeLayout *topRelativeLayout = [MyRelativeLayout new];
    topRelativeLayout.myLeftMargin = topRelativeLayout.myRightMargin = 0;
    topRelativeLayout.myHeight = 60;
    [rootLinearLayout addSubview:topRelativeLayout];

    self.headImage = [UIImageView new];
    _headImage.centerYPos.equalTo(topRelativeLayout.centerYPos);
    _headImage.leftPos.equalTo(topRelativeLayout.leftPos);
    _headImage.widthDime.equalTo(@40);
    _headImage.heightDime.equalTo(_headImage.widthDime);
    [topRelativeLayout addSubview:_headImage];
    _headImage.viewLayoutCompleteBlock = ^(MyBaseLayout *layout, UIView *sbv) {
        sbv.layer.cornerRadius = sbv.frame.size.width / 2;
        sbv.layer.masksToBounds = YES;
    };

    self.nickNameLabel = [UILabel new];
    _nickNameLabel.topPos.equalTo(_headImage.topPos);
    _nickNameLabel.leftPos.equalTo(_headImage.rightPos).offset(10);
    [topRelativeLayout addSubview:_nickNameLabel];
    
    self.genderImage = [UIImageView new];
    _genderImage.topPos.equalTo(_nickNameLabel.topPos);
    _genderImage.leftPos.equalTo(_nickNameLabel.rightPos).offset(10);
    _genderImage.widthDime.equalTo(@20);
    _genderImage.heightDime.equalTo(_genderImage.widthDime);
    [topRelativeLayout addSubview:_genderImage];
    
    self.ageLabel = [UILabel new];
    _ageLabel.topPos.equalTo(_genderImage.topPos);
    _ageLabel.leftPos.equalTo(_genderImage.rightPos);
    _ageLabel.bottomPos.equalTo(_genderImage.bottomPos);
    [topRelativeLayout addSubview:_ageLabel];
    _ageLabel.textColor = [UIColor whiteColor];
    
    self.maritalStatusLabel = [UILabel new];
    _maritalStatusLabel.leftPos.equalTo(_nickNameLabel.leftPos);
    _maritalStatusLabel.topPos.equalTo(_nickNameLabel.bottomPos).offset(10);
    [topRelativeLayout addSubview:_maritalStatusLabel];
    _maritalStatusLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    
    self.wantGoLabel = [UILabel new];
    _wantGoLabel.topPos.equalTo(_maritalStatusLabel.topPos);
    _wantGoLabel.leftPos.equalTo(_maritalStatusLabel.rightPos).offset(10);
    [topRelativeLayout addSubview:_wantGoLabel];
    _wantGoLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
    
    _remarkLabel = [UILabel new];
    _remarkLabel.myLeftMargin = _remarkLabel.myRightMargin = 0;
    _remarkLabel.myTopMargin = 10;
    _remarkLabel.flexedHeight = YES;
    _remarkLabel.numberOfLines = 0;
    [rootLinearLayout addSubview:_remarkLabel];
    
    MyRelativeLayout *picRelativeLayout = [MyRelativeLayout new];
    picRelativeLayout.myLeftMargin = picRelativeLayout.myRightMargin = 0;
    picRelativeLayout.myTopMargin = 10;
    picRelativeLayout.wrapContentHeight = YES;
    [rootLinearLayout addSubview:picRelativeLayout];
    
    self.imageView1 = [UIImageView new];
    _imageView1.leftPos.equalTo(picRelativeLayout.leftPos);
    _imageView1.topPos.equalTo(picRelativeLayout.topPos);
    _imageView1.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView1];
    _imageView1.tag = 301;
    
    self.imageView2 = [UIImageView new];
    _imageView2.leftPos.equalTo(_imageView1.rightPos).offset(10);
    _imageView2.topPos.equalTo(_imageView1.topPos);
    _imageView2.widthDime.equalTo(_imageView1.widthDime);
    _imageView2.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView2];
    _imageView2.tag = 302;

    self.imageView3 = [UIImageView new];
    _imageView3.leftPos.equalTo(_imageView2.rightPos).offset(10);
    _imageView3.topPos.equalTo(_imageView1.topPos);
    _imageView3.widthDime.equalTo(_imageView1.widthDime);
    _imageView3.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView3];
    _imageView3.tag = 303;
    
    _imageView1.widthDime.equalTo(@[_imageView2.widthDime.add(-10), _imageView3.widthDime.add(-10)]);
    
    self.imageView4 = [UIImageView new];
    _imageView4.leftPos.equalTo(_imageView1.leftPos);
    _imageView4.topPos.equalTo(_imageView1.bottomPos).offset(10);
    _imageView4.widthDime.equalTo(_imageView1.widthDime);
    _imageView4.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView4];
    _imageView4.tag = 304;
    
    self.imageView5 = [UIImageView new];
    _imageView5.leftPos.equalTo(_imageView4.rightPos).offset(10);
    _imageView5.topPos.equalTo(_imageView4.topPos);
    _imageView5.widthDime.equalTo(_imageView1.widthDime);
    _imageView5.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView5];
    _imageView5.tag = 305;
    
    self.imageView6 = [UIImageView new];
    _imageView6.leftPos.equalTo(_imageView5.rightPos).offset(10);
    _imageView6.topPos.equalTo(_imageView5.topPos);
    _imageView6.widthDime.equalTo(_imageView1.widthDime);
    _imageView6.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView6];
    _imageView6.tag = 306;
    
    self.imageView7 = [UIImageView new];
    _imageView7.leftPos.equalTo(_imageView1.leftPos);
    _imageView7.topPos.equalTo(_imageView4.bottomPos).offset(10);
    _imageView7.widthDime.equalTo(_imageView1.widthDime);
    _imageView7.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView7];
    _imageView7.tag = 307;
    
    self.imageView8 = [UIImageView new];
    _imageView8.leftPos.equalTo(_imageView7.rightPos).offset(10);
    _imageView8.topPos.equalTo(_imageView7.topPos);
    _imageView8.widthDime.equalTo(_imageView1.widthDime);
    _imageView8.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView8];
    _imageView8.tag = 308;
    
    self.imageView9 = [UIImageView new];
    _imageView9.leftPos.equalTo(_imageView8.rightPos).offset(10);
    _imageView9.topPos.equalTo(_imageView8.topPos);
    _imageView9.widthDime.equalTo(_imageView1.widthDime);
    _imageView9.heightDime.equalTo(_imageView1.widthDime);
    [picRelativeLayout addSubview:_imageView9];
    _imageView9.tag = 309;
    
    [self addImageTapGesture];
    
    MyRelativeLayout *backLayout = [MyRelativeLayout new];
    backLayout.myTopMargin = 10;
    backLayout.myLeftMargin = backLayout.myRightMargin = -10;
    backLayout.myHeight = 110;
    backLayout.backgroundColor = [UIColor colorWithWhite:0.878 alpha:1.000];
    [rootLinearLayout addSubview:backLayout];
    
    self.likeButton = [UIButton new];
    _likeButton.leftPos.equalTo(backLayout.leftPos);
    _likeButton.topPos.equalTo(backLayout.topPos).offset(2);
    _likeButton.widthDime.equalTo(@80);
    _likeButton.heightDime.equalTo(@40);
    [backLayout addSubview:_likeButton];
    _likeButton.backgroundColor = [UIColor whiteColor];
    [_likeButton addTarget:self action:@selector(LRJButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.replyButton = [UIButton new];
    _replyButton.leftPos.equalTo(_likeButton.rightPos);
    _replyButton.topPos.equalTo(_likeButton.topPos);
    _replyButton.widthDime.equalTo(_likeButton.widthDime);
    _replyButton.heightDime.equalTo(_likeButton.heightDime);
    [backLayout addSubview:_replyButton];
    [_replyButton addTarget:self action:@selector(LRJButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.joinButton = [UIButton new];
    _joinButton.leftPos.equalTo(_replyButton.rightPos);
    _joinButton.topPos.equalTo(_replyButton.topPos);
    _joinButton.widthDime.equalTo(_likeButton.widthDime);
    _joinButton.heightDime.equalTo(_likeButton.heightDime);
    [backLayout addSubview:_joinButton];
    [_joinButton addTarget:self action:@selector(LRJButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    fl.minimumInteritemSpacing = 20;
    fl.minimumLineSpacing = 10;
    self.LPJCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
    _LPJCollection.leftPos.equalTo(backLayout.leftPos);
    _LPJCollection.rightPos.equalTo(backLayout.rightPos);
    _LPJCollection.topPos.equalTo(_replyButton.bottomPos);
    _LPJCollection.bottomPos.equalTo(backLayout.bottomPos);
    _LPJCollection.backgroundColor = [UIColor whiteColor];
    _LPJCollection.showsHorizontalScrollIndicator = NO;
    _LPJCollection.showsVerticalScrollIndicator = NO;
    [backLayout addSubview:_LPJCollection];
    [_LPJCollection registerNib:[UINib nibWithNibName:@"LPJCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kLPJCollectionCell];
    _LPJCollection.dataSource = self;
    _LPJCollection.delegate = self;
    
    self.LPJInfoLabel = [UILabel new];
    _LPJInfoLabel.leftPos.equalTo(backLayout.leftPos);
    _LPJInfoLabel.rightPos.equalTo(backLayout.rightPos);
    _LPJInfoLabel.topPos.equalTo(_replyButton.bottomPos);
    _LPJInfoLabel.bottomPos.equalTo(backLayout.bottomPos);
    _LPJInfoLabel.backgroundColor = [UIColor whiteColor];
    _LPJInfoLabel.hidden = YES;
    _LPJInfoLabel.textAlignment = NSTextAlignmentCenter;
    _LPJInfoLabel.textColor = [UIColor colorWithWhite:0.878 alpha:1.000];
    [backLayout addSubview:_LPJInfoLabel];
    
    [rootLinearLayout layoutIfNeeded];
    self.tableView.tableHeaderView = rootLinearLayout;
}

- (void)addImageTapGesture {
    UITapGestureRecognizer *tapImage1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView1 addGestureRecognizer:tapImage1];
    _imageView1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView2 addGestureRecognizer:tapImage2];
    _imageView2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView3 addGestureRecognizer:tapImage3];
    _imageView3.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView4 addGestureRecognizer:tapImage4];
    _imageView4.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView5 addGestureRecognizer:tapImage5];
    _imageView5.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage6 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView6 addGestureRecognizer:tapImage6];
    _imageView6.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage7 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView7 addGestureRecognizer:tapImage7];
    _imageView7.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage8 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView8 addGestureRecognizer:tapImage8];
    _imageView8.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapImage9 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageAction:)];
    [_imageView9 addGestureRecognizer:tapImage9];
    _imageView9.userInteractionEnabled = YES;
}

#pragma mark - Data

- (void)createData {
    NSString *tocken = [[NSUserDefaults standardUserDefaults] objectForKey:@"tocken"];
    if (tocken.length == 0) {
        tocken = @"";
    }
    NSString *urlStr = [NSString stringWithFormat:@"http://api.miaotu.net/v2/yueyou?token=%@&yid=%zd", tocken, _yid];
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:urlStr andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.lineModel = [LineModel modelWithDic:result];
        self.travelItem = self.lineModel.Items;
        
        [self setData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)setData {
    [_headImage sd_setImageWithURL:[NSURL URLWithString:_travelItem.HeadUrl]];
    [_headImage sizeToFit];
    _nickNameLabel.text = _travelItem.Nickname;
    _nickNameLabel.font = [UIFont systemFontOfSize:19];
    [_nickNameLabel sizeToFit];
    if ([_travelItem.Gender isEqualToString:@"男"]) {
        _genderImage.backgroundColor = [UIColor colorWithRed:0.525 green:0.675 blue:0.922 alpha:1.000];
        _ageLabel.backgroundColor = [UIColor colorWithRed:0.525 green:0.675 blue:0.922 alpha:1.000];
        _genderImage.image = [UIImage imageNamed:@"man"];
    } else {
        _genderImage.backgroundColor = [UIColor colorWithRed:0.929 green:0.490 blue:0.608 alpha:1.000];
        _ageLabel.backgroundColor = [UIColor colorWithRed:0.929 green:0.490 blue:0.608 alpha:1.000];
        _genderImage.image = [UIImage imageNamed:@"woman"];
    }
    [_genderImage sizeToFit];
    _ageLabel.text = [NSString stringWithFormat:@"%zd岁", _travelItem.Age];
    _ageLabel.font = [UIFont systemFontOfSize:15];
    [_ageLabel sizeToFit];
    _maritalStatusLabel.font = [UIFont systemFontOfSize:13];
    _maritalStatusLabel.text = _travelItem.MaritalStatus;
    [_maritalStatusLabel sizeToFit];
    _wantGoLabel.font = [UIFont systemFontOfSize:13];
    _wantGoLabel.text = [NSString stringWithFormat:@"想去%@", _travelItem.WantGo];
    [_wantGoLabel sizeToFit];
    _remarkLabel.text = _travelItem.Remark;
    [_remarkLabel sizeToFit];
    self.picList = _travelItem.PicList;
    
    NSMutableArray<UIImageView *> *imageViewArray = [NSMutableArray array];
    [imageViewArray addObject:_imageView1];
    [imageViewArray addObject:_imageView2];
    [imageViewArray addObject:_imageView3];
    [imageViewArray addObject:_imageView4];
    [imageViewArray addObject:_imageView5];
    [imageViewArray addObject:_imageView6];
    [imageViewArray addObject:_imageView7];
    [imageViewArray addObject:_imageView8];
    [imageViewArray addObject:_imageView9];
    
    for (NSInteger i = 0; i < 9; i++) {
        if (_picList.count > i) {
            [imageViewArray[i] sd_setImageWithURL:[NSURL URLWithString:_picList[i].Url]];
            [imageViewArray[i] sizeToFit];
        } else {
            imageViewArray[i].hidden = YES;
        }
    }
    
    if (_travelItem.LikeList.count > 0) {
        [_likeButton setTitle:[NSString stringWithFormat:@"喜欢%zd", _travelItem.LikeList.count] forState:UIControlStateNormal];
    } else {
        [_likeButton setTitle:@"喜欢" forState:UIControlStateNormal];
    }
    [_likeButton setTitleColor:[UIColor colorWithWhite:0.506 alpha:1.000] forState:UIControlStateNormal];
    if (_travelItem.ReplyList.count > 0) {
        [_replyButton setTitle:[NSString stringWithFormat:@"评论%zd", _travelItem.ReplyList.count] forState:UIControlStateNormal];
    } else {
        [_replyButton setTitle:@"评论" forState:UIControlStateNormal];
    }
    [_replyButton setTitleColor:[UIColor colorWithWhite:0.506 alpha:1.000] forState:UIControlStateNormal];
    [_joinButton setTitle:[NSString stringWithFormat:@"已入伙%zd", _travelItem.JoinList.count] forState:UIControlStateNormal];
    [_joinButton setTitleColor:[UIColor colorWithWhite:0.506 alpha:1.000] forState:UIControlStateNormal];
    
    _userArray = _travelItem.LikeList;
    [_LPJCollection reloadData];
    
    if (_userArray.count == 0) {
        _LPJInfoLabel.hidden = NO;
        _LPJInfoLabel.text = @"还没有人喜欢过";
    }
    
    self.detailTitleArray = @[@[@"出发时间:", @"返回时间:", @"出 发 地:", @"目 的 地:"], @[@"费用说明:", @"求 约 伴:", @"约伴要求:"]];
    self.detailValueArray = @[@[_travelItem.StartDate, _travelItem.EndDate, _travelItem.From, _travelItem.Destination], @[_travelItem.MoneyType, @(_travelItem.Number).stringValue, _travelItem.Require]];
    [_tableView reloadData];
    [self.tableView.tableHeaderView layoutIfNeeded];
    self.tableView.tableHeaderView = self.tableView.tableHeaderView;
}

#pragma mark - CollectionView DataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_userArray.count > 6) {
        return 6;
    }
    return _userArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LPJCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLPJCollectionCell forIndexPath:indexPath];
    UserInfo *user = _userArray[indexPath.item];
    if (indexPath.item <= 4) {
        [cell.headImage sd_setImageWithURL:[NSURL URLWithString:user.HeadUrl]];
    } else {
        cell.headImage.image = [UIImage imageNamed:@"threeDot"];
    }
    return cell;
}

#pragma mark - CollectionViewFlowlayout Delegate

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(_LPJCollection.dk_height - 20, _LPJCollection.dk_height - 20);
}

#pragma mark - TableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 1) {
        return _detailTitleArray[section].count;
    }
    if (section == 2) {
        if (_travelItem.ReplyList.count == 0) {
            return 1;
        } else if (_travelItem.ReplyList.count > 3) {
            return 4;
        } else {
            return _travelItem.ReplyList.count;
        }
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        LineDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLineDetailTableCell];
        if (cell == nil) {
            cell = [[LineDetailTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kLineDetailTableCell];
        }
        if (indexPath.section == 0) {
            cell.titleLabel.textColor = [UIColor colorWithRed:0.153 green:0.341 blue:0.596 alpha:1.000];
            cell.valueLabel.textColor = [UIColor colorWithRed:0.153 green:0.341 blue:0.596 alpha:1.000];
        } else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.valueLabel.textColor = [UIColor colorWithRed:0.898 green:0.082 blue:0.000 alpha:1.000];
        }
        if (indexPath.section == 1 && indexPath.row == 0) {
            cell.orangeView.hidden = NO;
        } else {
            cell.orangeView.hidden = YES;
        }
        if ((indexPath.section == 0 && indexPath.row == 3) ||
            (indexPath.section == 1 && indexPath.row == 2)) {
            cell.grayView.hidden = YES;
        } else {
            cell.grayView.hidden = NO;
        }
        cell.titleLabel.text = _detailTitleArray[indexPath.section][indexPath.row];
        [cell.titleLabel sizeToFit];
        cell.valueLabel.text = _detailValueArray[indexPath.section][indexPath.row];
        [cell.valueLabel sizeToFit];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (indexPath.section == 2) {
        if (_travelItem.ReplyList.count == 0) {
            LineDefaultTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLineDefaultTableCell];
            if (cell == nil) {
                cell = [[LineDefaultTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kLineDefaultTableCell];
            }
            cell.labelText = @"肥美的评论处女地等你开垦\n快做第一个尝鲜者吧!";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        if (indexPath.row >= 0 && indexPath.row <= 2) {
            LineReplyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLineReplyTableCell];
            if (cell == nil) {
                cell = [[LineReplyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kLineReplyTableCell];
            }
            cell.userInfo = _travelItem.ReplyList[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        } else {
            LineDefaultTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kLineDefaultTableCell];
            if (cell == nil) {
                cell = [[LineDefaultTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kLineDefaultTableCell];
            }
            cell.labelText = [NSString stringWithFormat:@"点击查看,全部%zd条评论>>", _travelItem.ReplyList.count];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    return nil;
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 1) {
        LineDetailTableCell *cell = (LineDetailTableCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
        CGRect rect = [cell.rootLayout estimateLayoutRect:CGSizeMake(tableView.frame.size.width, 0)];
        return rect.size.height;
    }
    if (indexPath.section == 2) {
        if (indexPath.row >= 0 && indexPath.row <= 2) {
            if (_travelItem.ReplyList.count > 0) {
                LineReplyTableCell *cell = (LineReplyTableCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
                CGRect rect = [cell.rootLayout estimateLayoutRect:CGSizeMake(tableView.frame.size.width, 0)];
                return rect.size.height;
        }        
        } else {
            return 70;
        }
    }
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 70;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        UIView *remarkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.dk_width, 70)];
        remarkView.backgroundColor = [UIColor whiteColor];
        UILabel *remarkLabel = [UILabel new];
        [remarkView addSubview:remarkLabel];
        [remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(remarkView.mas_centerY);
            make.left.equalTo(remarkView.mas_left).offset(8);
        }];
        remarkLabel.text = @"评论";
        remarkLabel.textColor = [UIColor colorWithWhite:0.718 alpha:1.000];
        
        UIButton *reportButton = [UIButton new];
        [remarkView addSubview:reportButton];
        [reportButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(remarkView.mas_centerY);
            make.right.equalTo(remarkView.mas_right).offset(30);
        }];
        [reportButton setTitle:@"    举报路线           " forState:UIControlStateNormal];
        [reportButton setTitleColor:[UIColor colorWithWhite:0.733 alpha:1.000] forState:UIControlStateNormal];
        reportButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
        
        reportButton.layer.cornerRadius = 14.f;
        reportButton.layer.masksToBounds = YES;
        reportButton.layer.borderColor = [UIColor colorWithWhite:0.733 alpha:1.000].CGColor;
        reportButton.layer.borderWidth = 1;
        return remarkView;
    }
    return nil;
}

#pragma mark - Action

- (void)LRJButtonAction:(UIButton *)button {
    if (button == _likeButton) {
        _likeButton.backgroundColor = [UIColor whiteColor];
        _replyButton.backgroundColor = [UIColor clearColor];
        _joinButton.backgroundColor = [UIColor clearColor];
        _userArray = _travelItem.LikeList;
        _LPJInfoLabel.text = @"还没有人喜欢过";
    } else if (button == _replyButton) {
        _replyButton.backgroundColor = [UIColor whiteColor];
        _likeButton.backgroundColor = [UIColor clearColor];
        _joinButton.backgroundColor = [UIColor clearColor];
        _userArray = _travelItem.ReplyList;
        _LPJInfoLabel.text = @"还没有人评论过";
    } else if (button == _joinButton) {
        _joinButton.backgroundColor = [UIColor whiteColor];
        _likeButton.backgroundColor = [UIColor clearColor];
        _replyButton.backgroundColor = [UIColor clearColor];
        _userArray = _travelItem.JoinList;
    }
    if (_userArray.count == 0) {
        _LPJInfoLabel.hidden = NO;
    } else {
        _LPJInfoLabel.hidden = YES;
    }
    [_LPJCollection reloadData];
}

- (void)tapImageAction:(UITapGestureRecognizer *)tap {
    UIImageView *imageView = (UIImageView *)tap.view;
    NSArray<Piclist *> *picArray = _travelItem.PicList;
    NSInteger currentImageIndex = imageView.tag - 301;
    
    NSDictionary *dic = @{@"picArray": picArray, @"currentImageIndex": @(currentImageIndex)};
    PhotoAlbumViewController *photoAlbumVC = [[PhotoAlbumViewController alloc] init];
    photoAlbumVC.picDic = dic;
    [self.navigationController pushViewController:photoAlbumVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
